﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace gk.Games.SudokuSolver
{
    public class SudokuCell
    {
        public List<int> PossibleValues { get; set; }
        public int? Value { get; private set; }
        public List<IValueChecker> Validators { get; set; }

        public event EventHandler OnValueSet;

        public SudokuCell()
        {
            PossibleValues = new List<int>(9);
            Validators = new List<IValueChecker>(3);
            Reset();
        }

        public void Reset()
        {
            PossibleValues.Clear();
            PossibleValues.AddRange(new List<int>{1,2,3,4,5,6,7,8,9});
            Value = null;
        }

        public void SetValue(int? newValue)
        {            
            PossibleValues.Clear();
            Value = newValue;
            if (OnValueSet != null)
                OnValueSet(this, null);
        }

        public bool CanAcceptValue(int newValue)
        {
            foreach (var validator in Validators)
            {
                if(!validator.CanAddValue(newValue)) return false;
            }
            return true;
        }
    }
}
